package besta.moon.sql.expression.function;

import besta.moon.metadata.MoonResultSetMetaData;
import besta.moon.sql.datatype.Money;
import besta.moon.sql.datatype.MutableNumeric;
import besta.moon.util.MoonUtils;

public abstract class FunctionReturnP1StringAndBinary extends FunctionReturnP1 {

    @Override
    public final boolean getBoolean() throws Exception {
        if (isNull()) {
            return false;
        }
        return MoonUtils.string2boolean(getString().trim());
    }

    @Override
    public final int getInt() throws Exception {
        if (isNull()) {
            return 0;
        }
        return Integer.parseInt(getString().trim());
    }

    @Override
    public final long getLong() throws Exception {
        if (isNull()) {
            return 0;
        }
        return Long.parseLong(getString().trim());
    }

    @Override
    public final float getFloat() throws Exception {
        if (isNull()) {
            return 0;
        }
        return Float.parseFloat(getString().trim());
    }

    @Override
    public final double getDouble() throws Exception {
        if (isNull()) {
            return 0;
        }
        return Double.parseDouble(getString().trim());
    }

    @Override
    public final long getMoney() throws Exception {
        if (isNull()) {
            return 0;
        }
        return Money.parseMoney(getString().trim());
    }

    @Override
    public final MutableNumeric getNumeric() throws Exception {
        if (isNull()) {
            return null;
        }
        return new MutableNumeric(getString().trim());
    }

    @Override
    public final Object getObject() throws Exception {
        if (MoonResultSetMetaData.isBinaryDataType(param1.getDataType())) {
            return getBytes();
        }
        return getString();
    }
}
